// JavaScript Document
function formSubmit(formName){
	eval('document.'+ formName + '.submit();');
}

function getFileExtension($file){
	return ($file.substring($file.lastIndexOf('.') + 1)).toLowerCase();
}
function open_popup(url,popup_name,width,height){
	var left = Math.floor((screen.width - width) / 2);
	var top = Math.floor((screen.height - height) / 2);
	window.open(url,popup_name, 'height='+height+', width='+width+', left='+left+',top='+top+',toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, status=no');
}
function redir(url){
	eval("location.href = '" + url + "'");
}
function redirParent(url){
	eval("opener.location.href = '" + url + "'");
	self.close();
}
function check_all(obj, objImage) {
	if (obj) {
		if (obj.length) {
			value = obj[0].checked==false?true:false;
			
			for (i=0;i<obj.length;i++)
				obj[i].checked = value;
		}
		else {
			value = obj.checked==false?true:false;
			obj.checked = value;
		}
		/**/
		if(objImage){
			objImage.src = (value) ? srcImageClearAll : srcImageCheckAll; 
		}
		
	}
}

function ClearSelectBox(obj){
	var NumState = obj.options.length;
	while(NumState > 0){
		NumState--;
		obj.options[NumState] = null;
	}
}



/**
 * COOKIE
 */
// Boolean variable specified if alert should be displayed if cookie exceeds 4KB
var caution = false
/* 
 name - name of the cookie
 value - value of the cookie
 [expires] - expiration date of the cookie (defaults to end of current session)
 [path] - path for which the cookie is valid (defaults to path of calling document)
 [domain] - domain for which the cookie is valid (defaults to domain of calling document)
 [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
 * an argument defaults when it is assigned null as a placeholder
 * a null placeholder is not required for trailing omitted arguments
 */
function setCookie(name, value, expires, path, domain, secure)
{
var curCookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "")
if (!caution || (name + "=" + escape(value)).length <= 4000)
document.cookie = curCookie
else
if (confirm("Cookie exceeds 4KB and will be cut!"))
document.cookie = curCookie
}
// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name)
{
var prefix = name + "="
var cookieStartIndex = document.cookie.indexOf(prefix)
if (cookieStartIndex == -1)
return null
var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex +
prefix.length)
if (cookieEndIndex == -1)
cookieEndIndex = document.cookie.length
return unescape(document.cookie.substring(cookieStartIndex +
prefix.length, cookieEndIndex))
}
// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds

function deleteCookie(name, path, domain)
{
	if (getCookie(name))
	{
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT"
	}
}
function sendWithAction( FormName, sAction ){
	if(document.forms[FormName]){
		document.forms[FormName].action = sAction;
		document.forms[FormName].submit();
	}	
	if(document.FormName){
		document.FormName.action = sAction;	
		document.FormName.submit();
	}	

}

function send(FormName){
	 alert('FormName');
	if(document.forms[FormName]){
		document.forms[FormName].submit();
	}	
	if(document.FormName){
		document.FormName.submit();
	}
}

function sendID(id){
	if(document.getElementById(id)){
		document.getElementById(id).submit();
	}
}

function display(sMsg,sID){
	try{
		document.getElementById(sID).innerHTML=sMsg;
	}
	catch(e){
		try{
			document.sID.innerHTML=sMsg;
		}
		catch(e){
			try{
				document.all.sID.innerHTML=sMsg;
			}
			catch(e){
				alert(sMsg);
			}
		}
	}
}
	
function validateNumeric(Num){
	er=new RegExp("^[0-9]+$");
	return er.test(Num);
}

function validateDate(day,month,year){
	date=new Date();
	yearNow=1900+date.getYear();
	if(year==''||month==''||day==''){
		return false;
	}
	if(isNumber(year)||isNumber(month)||isNumber(day)){
		return false;
	}
	if(month<1||month>12){
		return false;
	}if(year>yearNow){
		return false;
	}
	
	var dayInFebruary=checkLeapYear(year)?29:28;
	var daysInMonth=new Array('Days',31,dayInFebruary,31,30,31,30,31,31,30,31,30,31);
	if(month<10&&month.length==2)month=month.substring(1);
	if(day>0&&day<=daysInMonth[month]){
		return true;
	}else{
		return false;
	}
}

function checkLeapYear(year){
	return(((year%4==0)&&((!(year%100==0))||(year%400==0)))?true:false);
}
function validateEmail(email){
	er=new RegExp("^([A-Za-z0-9.-_-]+)@[A-Za-z0-9-_]+(\.[A-Za-z0-9]+)?\.([A-Za-z]){2,4}$");
	return er.test(email);
}

function OpenWindow(URL,title,features,width,height,isCenter){
	var left=0;
	var top=0;
	if((window.screen)&&isCenter){
		var left=(window.screen.width/2)-(width/2);
		var top=(window.screen.height/2)-(height/2);
	}
	features+=(features!='')?',':'';
	features+='left='+left+',top='+top+',width='+width+',height='+height;
	window.open(URL,title,features);
}

function removeChar(characters,text){
	var ntext='';
	if(characters.length>0){
		for(i=0;i<text.length;i++){
			exist=false;
			for(j=0;j<characters.length;j++){
				if(text.charAt(i)==characters[j]){
					exist=true;
					break;
				}
			}
			if(!exist){
				ntext+=text.charAt(i);
			}
		}
		return ntext;
	}else{
		return text;
	}
}

function addClass(sID,sClassName){
	var oID=document.getElementById(sID);
	if(oID.className===''){
		oID.className=sClassName;
	}else{
		oID.className=oID.className+', '+sClassName;
	}
}

function removeClass(sID,sClassName){
	var oID=document.getElementById(sID);
	var aClass=oID.className.split(', ');
	oID.className='';
	for(i=0;i<=aClass.length;i++){
		if(!(aClass[i]===sClassName)&&typeof(aClass[i])!='undefined'){
			addClass(sID,aClass[i]);
		}
	}
}

function selectAll(){
	fields=document.getElementsByTagName('input');
	for(i=0;i<fields.length;i++){
		if(fields[i].type=='checkbox'){
			fields[i].checked=true;
		}
	}
	fields=document.getElementsByTagName('tr');
	for(i=0;i<fields.length;i++){
		if(fields[i].id.charAt(0)==='t'&&fields[i].id.charAt(1)==='r')addClass(fields[i].id,'highlight3');
	}
}

function unselectAll(){
	fields=document.getElementsByTagName('input');
	for(i=0;i<fields.length;i++){
		if(fields[i].type=='checkbox'){
			fields[i].checked=false;
		}
	}
	fields=document.getElementsByTagName('tr');
	for(j=0;j<fields.length;j++){
		if(fields[j].id.charAt(0)==='t'&&fields[j].id.charAt(1)==='r')removeClass(fields[j].id,'highlight3');
	}
}

function select(){
	fields=document.getElementsByTagName('input');
	for(i=0;i<fields.length;i++){
		if(fields[i].type=='checkbox'){
			if(fields[i].checked==false){
				selectAll();
				return;
			}
		}
	}
	unselectAll();
}

function mountMessages(sMsgs){
	aMsgs=sMsgs.split('||');
	aNewMsgs=Array();
	for(i=0;i<aMsgs.length;i++){
		aAux=aMsgs[i].split(':++:');
		aNewMsgs[aAux[0]]=aAux[1];
	}
	return aNewMsgs;
}

function getElement(sId,bUseOpener){
	if(bUseOpener){
		if(document.getElementById){
			return window.opener.document.getElementById(sId);
		}else{
			if(document.layers){
				return window.opener.document.sId;
			}else{
				return window.opener.document.all.sId;
			}
		}
	}else{
		if(document.getElementById){
			return document.getElementById(sId);
		}else{
			if(document.layers){
				return document.sId;
			}else{
				return document.all.sId;
			}
		}
	}
}

/*
Function validateFields(aCheck)
	Note: aCheck[i][0] = field ID,
		  aCheck[i][1] = minimum character
		  aCheck[i][2] = maximum character
		  aCheck[i][3] = error message
		  aCheck[i][4] = Is numberic
		  aCheck[i][5] = div ID of error message.  (if not found div id will appear a window message box
													
Sample to use:
var aCheck = new Array();
	/*
	Note: aCheck[i][0] = field ID,
		  aCheck[i][1] = minimum character
		  aCheck[i][2] = maximum character
		  aCheck[i][3] = error message
		  aCheck[i][4] = Is numberic
		  aCheck[i][5] =Is_show_message_follow_template, if == true: then show default message
		  aCheck[i][6] = div ID of error message.  (if not found div id will appear a window message box
	
	aCheck.push ( new Array('store_id', 1, 200, "{LOGIN.store_empty}", false, false, 'form_room_name') );
	aCheck.push ( new Array('password', 1, 200, "{LOGIN.password_empty}", false, false,'form_password') );
	aCheck.push ( new Array('UserId', 1, 200, "{LOGIN.password_empty}", false, false,'form_UserId' ) );
	var bRes = validateFields(aCheck);
	if(!bRes)
	{
		return bRes; // return false
		
	}
	else
	{			
        return bRes; // return true
	}	
*/	

function validateFields(aCheck){
	for(i=0;i<aCheck.length;i++){
		if((document.getElementById(aCheck[i][0]))){
			var obj=document.getElementById(aCheck[i][0]).value;
			var size=obj.length;
			
			if(aCheck[i][1]==0&&aCheck[i][2]==0&&obj===''){
				if(aCheck[i][5]){
					display('Field '+aCheck[i][3]+' cannot be blank',aCheck[i][6],'att');
					//scrollToByElement('msgs');	
				}else{
					display(aCheck[i][3],aCheck[i][6],'att');
						//scrollToByElement('msgs');
				}
				return false;
			}else if(aCheck[i][4]&&isNaN(obj)){
				if(aCheck[i][5]){
					display('Field '+aCheck[i][3]+' must Accountin only number.',aCheck[i][6],'att');
					//scrollToByElement('msgs');
				}else{
					display(aCheck[i][3],aCheck[i][6],'att');
					//scrollToByElement('msgs');
				}
				return false;
			}else if(aCheck[i][1]>0&&aCheck[i][2]&&(size<aCheck[i][1]||size>aCheck[i][2])){
				if(aCheck[i][5]){
					display('Field '+aCheck[i][3]+' must Accountin value in maximum '+aCheck[i][2]+' and minimum '+aCheck[i][1]+' characters.',aCheck[i][6],'att');
					scrollToByElement('msgs');
				}else{
					display(aCheck[i][3],aCheck[i][6],'att');
					//scrollToByElement('msgs');
				}
				return false;
			}
		}
	}
	return true;
}

/*function print(sMsg,sID,sClass){
	try{
		document.getElementById(sID).innerHTML=sMsg;
		if(sClass)addClass(sID,sClass)
	}
	catch(e){
		try{document.sID.innerHTML=sMsg;
		if(sClass)addClass(sID,sClass)
		}
		catch(e){
			try{
				document.all.sID.innerHTML=sMsg;
				if(sClass)addClass(sID,sClass)
			}
			catch(e){
				alert(sMsg);
			}
		}
	}
}*/

function addClass(sID,sClassName){
	if(document.getElementById){
		oID=document.getElementById(sID);
	}else{
		if(document.layers){
			oID=document.sID;
		}else{
			oID=document.all.sID;
		}
	}
	if(oID.className===''){
		oID.className=sClassName;
	}else{
		oID.className=oID.className+' '+sClassName;
	}
}

function removeClass(sID,sClassName){
	if(document.getElementById){
		oID=document.getElementById(sID);
	}else{
		if(document.layers){
			oID=document.sID;
		}else{
			oID=document.all.sID;
		}
	}
	
	var aClass=oID.className.split(' ');
	oID.className='';
	for(i=0;i<=aClass.length;i++){
		if(!(aClass[i]===sClassName)&&typeof(aClass[i])!='undefined'){
			addClass(sID,aClass[i]);
		}
	}
}

function checkByID(sID,sTRID){
	if(document.getElementById){
		oID=document.getElementById(sID);
	}else{
		if(document.layers){
			oID=document.sID;
		}else{
			oID=document.all.sID;
		}
	}
	if(oID.checked===true){
		oID.checked=false;
		removeClass(sTRID,'highlight3');
	}else{
		oID.checked=true;
		addClass(sTRID,'highlight3');
	}
}


function countChars(eFrom,iIdTo){
	var countFrom=eFrom;
	var printIn=getElement(iIdTo);
	printIn.value=countFrom.value.length;
}

function openURLOpener(sURL,bClose){
	window.opener.location.href=sURL;
	if(bClose==true){
		window.close();
	}
}

function openURL(sURL,bClose){
	window.location.href=sURL;
	if(bClose==true){
		window.close();
	}
}

function protegerEmail(dominio,extensao,usuario){
	document.write("<a href="+"mail"+"to:"+usuario+"@"+dominio+extensao+">"+usuario+"@"+dominio+extensao+"</a>");
}


function scrollToByElement(sID){
	if ( !getElement(sID) ) return;
	var eOBJ=getElement(sID);
	var iWidth=encontrarPosX(eOBJ);
	var iHeight=encontrarPosY(eOBJ);
	window.scrollTo(iWidth,iHeight);
}

function encontrarPosY(eOBJ){
	var iPosTop=0;
	if(eOBJ.offsetParent){
		while(eOBJ.offsetParent){
			iPosTop+=eOBJ.offsetTop;
			eOBJ=eOBJ.offsetParent;
		}
	}else if(eOBJ.y){
		iPosTop+=eOBJ.y;
	}
	return iPosTop;
}

function encontrarPosX(eOBJ){
	var iPosLeft=0;
	if(eOBJ.offsetParent){
		while(eOBJ.offsetParent){
			iPosLeft+=eOBJ.offsetLeft;
			eOBJ=eOBJ.offsetParent;
		}
	}else if(eOBJ.x){
		iPosLeft+=eOBJ.x;
	}
	return iPosLeft;
}

/*
   function: hide
      Hide an Element

   parameters:

      id - ID of Element
*/
function hide(id) {
    if (document.getElementById) // DOM3 = IE5, NS6
    {
        document.getElementById(id).style.display = 'none';
        document.getElementById(id).style.visibility = 'hidden';
    }
    else 
    {
        if (document.layers) // Netscape 4
        {
            document.id.display = 'none';
            document.id.visibility = 'hidden';
        }
        else // IE 4
        { 
            document.all.hideshow.style.display = 'none';
            document.all.hideshow.style.visibility = 'hidden';
        }
    }
}

/*
   function: show
      Show an Element

   parameters:

      id - ID of Element
      sDisplayProp - Type of display that allows modify. Block, table, table-row
*/

function show(id, sDisplayProp ) 
{
    if ( typeof( sDisplayProp ) == "undefined" )
        sDisplayProp = 'block';

    if (document.getElementById) // DOM3 = IE5, NS6
    {
        try { 
            document.getElementById(id).style.display = sDisplayProp;
            document.getElementById(id).style.visibility = 'visible';
        }
        catch(e)
        { // current tables has problems with IE
            document.getElementById(id).style.display = 'block';
            document.getElementById(id).style.visibility = 'visible';
        }
    }
    else
    {
        if (document.layers) // Netscape 4
        {
            document.id.display = sDisplayProp;
            document.id.visibility = 'visible';
        }
        else // IE 4
        {
            document.all.id.style.display = sDisplayProp;
            document.all.id.style.visibility = 'visible';
        }
    }
}

/*
function: showhide
    Similar Hide with accordancing a div
    
parameters:

      id - ID of Element
      sDisplayProp - Type of display that allows modify. Block, table, table-row
*/
function showhide( id, sDisplayProp )
{
    if (document.getElementById) // DOM3 = IE5, NS6
    {
        if ( document.getElementById(id).style.display == 'none' )
            show( id, sDisplayProp );
        else
            hide( id );
    }
    else
    {
        if (document.layers) // Netscape 4
        {
            if ( document.id.display == 'none' )
                show( id, sDisplayProp );
            else
                hide( id );
        }
        else // IE 4
        {
            if ( document.all.id.style.display == 'none' )
                show( id, sDisplayProp );
            else
                hide( id );
        }
    }
}

/*
    function: setFocus
       Set focus

    parameters:

       string:

       FormName - 
       FieldName - 
*/
function setFocus( sFormName, sFieldName )
{
    if (document.forms[sFormName])
    {
    document.forms[sFormName][sFieldName].focus();
    }
    else if (document.sFormName)
    {
    document.sFormName.sFieldName.focus();
    }
}

function inspect(elm){
  var str = "";
  for (var i in elm){
    str += i + ": " + elm.getAttribute(i) + "\n";
  }
  alert(str);
}

// Source: http://www.mikezilla.com/exp0015.html

function addZero(vNumber){ 
    return ((vNumber < 10) ? "0" : "") + vNumber 
} 

function formatDate(vDate, vFormat){ 
    var vDay              = addZero(vDate.getDate()); 
    var vMonth            = addZero(vDate.getMonth()+1); 
    var vYearLong         = addZero(vDate.getFullYear()); 
    var vYearShort        = addZero(vDate.getFullYear().toString().substring(3,4)); 
    var vYear             = (vFormat.indexOf("yyyy")>-1?vYearLong:vYearShort) 
    var vHour             = addZero(vDate.getHours()); 
    var vMinute           = addZero(vDate.getMinutes()); 
    var vSecond           = addZero(vDate.getSeconds()); 
    var vDateString       = vFormat.replace(/dd/g, vDay).replace(/MM/g, vMonth).replace(/y{1,4}/g, vYear) 
    vDateString           = vDateString.replace(/hh/g, vHour).replace(/mm/g, vMinute).replace(/ss/g, vSecond) 
    return vDateString 
} 

/*
function: CatchPageQuery
    Return result or definative variable of a query
    
parameters:

    sVariable - 
    
returns:
        
        mixed
*/
function CatchPageQuery( sVariable ) {
  var sEndereco = window.location.search.substring(1);
  aVars = sEndereco.split( '&' );
  
  if ( typeof( sVariable ) == 'undefined' ) 
    return aVars;
  
  for (var i=0;i<aVars.length;i++) 
  {
    var sPar = aVars[i].split("=");
    
    if ( sPar[0] == sVariable ) {
      return sPar[1];
    }
  } 
  
  return false;
}

function stripSpaces(x) {
    while (x.substring(0,1) == ' ') x = x.substring(1);
    return x;
}

function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
    return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else
  {
    return document.getElementById(movieName);
  }
}

function GotoFrameFlashMovie(FrameNumber)
{
    var flashMovie=getFlashMovieObject("alerts");
    flashMovie.GotoFrame(FrameNumber);
}

function tocarSom( cNada, sTipo )
{
    if ( sTipo == 'som' )
    {
        GotoFrameFlashMovie(2);
    }
    else
    {
        GotoFrameFlashMovie(1);
    }
}

// Source: http://www.codebrain.com/javascript/kits/index.html
function tocarSomJS( doWhat, toWhat )
{
    var A = eval('document.'+toWhat);

    if (A != null)
    {
        if (doWhat=='stop')
        {
            A.stop();
        }
        else
        {
            if (navigator.appName == 'Netscape')
            {
                A.play();
            }
            else
            {
                if (document.M == null)
                {
                    document.M = false; 
                    var m;
                    for(m in A)
                    {
                        if (m == "ActiveMovie")
                        {
                            document.M = true; 
                            break;
                        }
                    }
                }
                
                if (document.M)
                {
                    A.SelectionStart = 0;
                }
                
                if (document.M) 
                {
                    A.play();
                }
            }
        }
    }
}

function trimAll(sString)
{
    while (sString.substring(0,1) == ' ')
    {
        sString = sString.substring(1, sString.length);
    }
    
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}

//////////////   TuNN added 15-Dec-08 ////////////////
function checkFloatFormat(n,d)
{
	if(isNaN(n) || n=='') return false;
	
	var dot = n.indexOf('.');
	var len = n.length;
	if(dot==-1)
		return true;
	if(dot==len-1)
		return false;
	if(n.indexOf('.')<len-(d+1))
		return false;
	return true;
}

function check_2decimal(strAmount)
{
	 var Amount=0;
		 
		 var check_decimal=0;
		 var myString = new String(strAmount);
		 var myStringList = new Array(strAmount); 
		 var myStringList1 = myString.split(' ');
		 
		 
         for(var i=0;i<myStringList1.length;i++)
		 {
		 
		 	if(myStringList1[i]==null || myStringList1[i]=='')
			{
				myStringList1[i]=0;
			}
			else
			{
			
				if(myStringList1[i].toString().indexOf(".")!=-1)
				{
					var temp_a=myStringList1[i].toString().split('.');
					if(temp_a[1].length >2)
					{
					 check_decimal++;
					}
				}
			}
			Amount = Amount + parseFloat(myStringList1[i]);
			
		}
		if(check_decimal!=0)
		{
			 print('Please input 2 decimal in Amount','msg');
			return false;
		}
		else
		{
		print('&nbsp;','msg');
		return Amount;
		}
}